#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 110;
int n, m;
int dis[N][N];
void floyd()
{
	for (int k = 1; k <= n; k++) {
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
			}
		}
	}
}
int main()
{
	while (cin >> n >> m) {
		if (!n && !m) {
			break;
		}
		memset(dis, 0x3f, sizeof(dis));
		while (m--) {
			int a, b, c;
			cin >> a >> b >> c;
			dis[a][b] = dis[b][a] = min(dis[a][b], c);
		}
		floyd();
		cout << dis[1][n] << endl;
	}
	return 0;
}